
import {clamp} from "../util/math.js";
import {$} from "../util/seq.js";

const REMAINING = "▫▫▫▫▫▫▫▫▫▫";   // glyphs for remaining progress bar
const COMPLETED = "▪▪▪▪▪▪▪▪▪▪";   // glyphs for completed progress bar
const total = REMAINING.length;

/**
 * @param bar
 * @param {Handle} handle
 */
export function defineProgressbar(bar, handle) {
    handle.on($`change`, pct => {
        pct = clamp(+pct, 0, 1);
        bar.property("hidden", pct !== pct);
        bar.attr("aria-valuenow", pct === pct ? pct : null);

        const i = Math.floor(pct * total);
        bar.text(COMPLETED.substr(0, i) + REMAINING.substr(0, total - i));
    });
}
